package com.gitee.mrj.aiboot.core.sf.generators.ui;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.SFHorizontalLayout;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFHorizontalLayoutGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFHorizontalLayout layout = AnnotatedElementUtils.findMergedAnnotation(field, SFHorizontalLayout.class);
        generate(node, layout);
    }

    @Override
    public void generate(UIForm node, Class clazz) {
        SFHorizontalLayout layout = AnnotatedElementUtils.findMergedAnnotation(clazz, SFHorizontalLayout.class);
        generate(node, layout);
    }

    private void generate(UIForm node, SFHorizontalLayout layout) {
        ObjectNode ui = node.getUi();
        putInt(ui, "offsetControl", layout.offsetControl());
        putInt(ui, "spanLabel", layout.spanLabel());
        if (layout.spanLabel() >= 0 && layout.spanLabel() <= 24) {
            putInt(ui, "spanControl", 24 - layout.spanLabel());
        }
        putInt(ui, "spanLabelFixed", layout.spanLabelFixed());
    }


    @Override
    public Class[] supports() {
        return new Class[]{SFHorizontalLayout.class};
    }
}
